/* async function t1() {
  // await 是一个表达式，如果后面不是一个 promise 对象，就直接返回对应的值
  let a = await "lagou"; // 当前代码执行栈
  return a; // 微任务代码中
}

console.log(t1().then((v) => console.log(v))); */

/* function* t1() {
  let a = yield "lagou";
  return a;
}

console.log(co(t1).then((v) => console.log(v)));

function co(generator) {
  const g = generator();
  function hanldeResult(result) {
    if (result.done) {
      return Promise.resolve(result.value);
    }
    // 如果 yield 后面的值不是 Promise 对象，保证成 Promise 对象
    if (!(result.value instanceof Promise)) {
      result.value = Promise.resolve(result.value);
    }
    return result.value.then(function (data) {
      hanldeResult(g.next(data));
    });
  }

  return hanldeResult(g.next());
} */

// 问题2
/* async function t2() {
  // resolve状态未改变，一直都是pending状态，因此不会输出a

  const a = await new Promise((resolve) => {});
  console.log(a);
}
t2();

function* t2() {
  let a = yield new Promise((resolve) => {});
  console.log(a);
}
const generator = t2();
const result = generator.next();
result.value.then((v) => {
  generator.next(v);
}); */

// 问题3 DACGIBHF
async function async1() {
  console.log("A");
  // async2();
  await async2();
  console.log("B"); // await后的代码本质时.then中的代码，因此这里是下一个微任务
}

async function async2() {
  console.log("C");
}

console.log("D");
setTimeout(() => {
  console.log("F");
}, 0);
async1();
new Promise((resolve) => {
  console.log("G");
  resolve();
}).then(() => {
  console.log("H");
});
console.log("I");
